﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NetworkSupervisor.Helper;
using NetworkSupervisor.Model.NetworkSupervisor;
using System.Linq;

namespace NetworkSupervisor
{
    public partial class FormAttentionSearch : DevExpress.XtraEditors.XtraForm
    {
        private bool stopIt;

        public FormAttentionSearch()
        {
            InitializeComponent();
        }

        private int i = 0;

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount < 1)
            {
                XtraMessageBox.Show("暂无关注信息");
                return;
            }

            stopIt = false;
            progressBarControl1.Position = i = 0;
            btnStart.Enabled = false;
            btnPause.Enabled = true;

            System.Threading.ThreadPool.QueueUserWorkItem(callBack);
        }

        private void callBack(object state)
        {
            while (true)
            {
                try
                {
                    foreach (var item1 in XPC网址)
                    {
                        ta网址 item = item1 as ta网址;
                        if (string.IsNullOrEmpty(item.网址))
                        {
                            Thread.Sleep(1000);
                            continue;
                        }


                        IList<ArticleInfo> articleList = null;
                        try
                        {
                            articleList = SearchHelper.GetLatestArticleList(item.网址, 0, "关注");
                        }
                        catch (Exception ex)
                        {
                        }
                        foreach (ArticleInfo info in articleList)
                        {
                            bool isNew = XPC搜索结果.Cast<ta搜索结果>().All(o => o.标题 != info.Title);
                            if (isNew)
                            {
                                ta搜索结果 itemJG = new ta搜索结果(session1);
                                itemJG.标题 = info.Title;
                                itemJG.链接 = info.Href;
                                itemJG.收录时间 = info.Datetime;
                                itemJG.关键字 = info.Keyword;
                                itemJG.类别 = info.Type;
                                itemJG.信息摘要 = info.Content;
                                itemJG.信息来源 = info.From;
                                itemJG.处理情况 = "待处理";
                                itemJG.Save();
                                try
                                {
                                    this.UIThread(() => XPC搜索结果.Add(itemJG));
                                }
                                catch
                                {
                                }
                            }

                            i++;
                            try
                            {
                                this.UIThread(() => progressBarControl1.Position = i);
                            }
                            catch
                            {
                            }
                            if (i == 100) i = 0;
                            System.Threading.Thread.Sleep(100);
                            if (stopIt)
                            {
                                progressBarControl1.Position = 0;
                                return;
                            }
                        }
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                catch
                {
                }
            }
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            progressBarControl1.Position = i = 0;
            btnStart.Enabled = true;
            btnPause.Enabled = false;
            stopIt = true;
        }

        private void FormAttentionSearch_Load(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnPause.Enabled = false;
            stopIt = false;
        }

        private void gridView2_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            gridView2.IndicatorWidth = 80;
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridView2_DoubleClick(object sender, EventArgs e)
        {
            if (gridView2.RowCount < 1) return;
            ta搜索结果 item = gridView2.GetFocusedRow() as ta搜索结果;
            System.Diagnostics.Process.Start(item.链接);
        }

        private void FormAttentionSearch_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (btnStart.Enabled == false)
            {
                XtraMessageBox.Show("正在监测，请先停止监测后再退出");
                e.Cancel = true;
            }
            else
            {
                stopIt = true;
            }
        }
    }
}